#include "filesavewnd.h"
#include <QtGui>
#include "touchlineedit.h"
#include "wndmanager.h"
#include "syscontroller.h"

FileSaveWnd::FileSaveWnd(QWidget *parent)
	: QWidget(parent),
	  sys_(SysController::instance())
{
	filePathEdit = new TouchLineEdit();
	filePathEdit->setText(sys_->programFilePath());
	suffixLabel = new QLabel(tr(".PRG"));
	okButton = new QPushButton(tr("OK"));
	cancelButton = new QPushButton(tr("Cancel"));
	hLayout = new QHBoxLayout;
	vLayout = new QVBoxLayout;
	hLayout->addWidget(filePathEdit);
	hLayout->addWidget(suffixLabel);
	hLayout->addWidget(cancelButton);
	hLayout->addWidget(okButton);
	vLayout->addLayout(hLayout);
	vLayout->addStretch();
	setLayout(vLayout);

	connect(okButton, SIGNAL(clicked()), this, SLOT(wndShouldReturn()));
	connect(cancelButton, SIGNAL(clicked()), this, SLOT(wndShouldReturn()));
}


FileSaveWnd::~FileSaveWnd()
{
	delete suffixLabel;
	delete filePathEdit;
	delete okButton;
	delete cancelButton;
	delete hLayout;
	delete vLayout;
}


void FileSaveWnd::wndShouldReturn()
{
	QPushButton *button = qobject_cast<QPushButton *>(sender());
	QString file_path = filePathEdit->text();
	if (button == okButton) {
		if (file_path.isEmpty()) {
			qDebug() << "NO VALID FILE PATH! CANNOT SAVE!!";
			return;
		} else {
			sys_->SaveProgram(file_path);	
		}
	}
	WndManager::gotoWnd(WndManager::kWndProgramWnd);
}
